package com.hao.epidemic_prevention.controller;

import com.hao.epidemic_prevention.bean.UserRecord;
import com.hao.epidemic_prevention.service.UserService;
import com.hao.epidemic_prevention.utils.JSONResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

/**
 * @ClassName UserController
 * @Descroption
 * @Author hao
 * @Date 2021/6/11 15:56
 **/

@Controller
@CrossOrigin(origins = "*")
@RequestMapping("/user")
public class UserController {
    @Autowired
    private UserService userService;

    @RequestMapping("/toIndex")
    public String toLogin(Model model) {
        if(model.getAttribute("user") == null) return new JSONResult(401, "未登录！", null).toString();
        return "index";
    }

    //@RequestBody UserRecord userRecord
    @ResponseBody
    @RequestMapping("/report")
    public JSONResult report(@RequestBody UserRecord userRecord) {
        String roleId = null;
        roleId = userRecord.getRoleId();
        if(roleId == null) {
            return new JSONResult(403, "没有User信息", null);
        }

        if(userService.isReportToday(roleId) == true) {
            return new JSONResult(500, "今天已经上传了，请勿重复上传", null);
        }

        Boolean result = userService.addUserRecord(userRecord);

        if(result)return new JSONResult(200, "上报成功", null);
        else return new JSONResult(500, "上报失败", null);
    }

    @ResponseBody
    @RequestMapping("/isReportToday")
    public JSONResult isReportToday(@RequestParam String roleId) {
        if(userService.isReportToday(roleId) == true) {
            return new JSONResult(200, "今天已上传", null);
        }
        else {
            return new JSONResult(201, "今天未上传", null);
        }
    }

    @ResponseBody
    @RequestMapping({"/isFreeze"})
    public JSONResult isFreeze(@RequestParam String roleId) {
        if(userService.isFreeze(roleId) == true) {
            return new JSONResult(201, "账号已冻结，请联系管理员", null);
        }
        else {
            return new JSONResult(200, "账号正常", null);
        }
    }
}
